﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTGroup.iNek.Business.FingerprintDatabase.Provider;
using SuperMap.Data;
using DTGroup.iNek.Utility;
using System.IO;

namespace DTGroup.iNek.WinUI._Codes
{
    public class SuperMapProviderImpl : ISuperMapProvider
    {
        private NLog.Logger logger = new NLog.LogFactory().GetCurrentClassLogger();

        /// <summary>
        /// 地图坐标转换为经纬度
        /// </summary>
        public GeometryPoint MapCoordSysConvertToLonLat(GeometryPoint tempPoint)
        {
            using (GeoPoint point = new GeoPoint(tempPoint.STX, tempPoint.STY))
            {
                // 地图 投影坐标 转换为经纬度
                CoordSysTranslator.Convert(point, ProjectCoordSysType.MapPrjCoordSys, ProjectCoordSysType.LatLonPrjCoordSys, new CoordSysTransParameter(), CoordSysTransMethod.CoordinateFrame);
                return new GeometryPoint(point.X, point.Y);
            };
        }

        /// <summary>
        /// 经纬度转换为地图一致的坐标
        /// </summary>
        public GeometryPoint LonLatCoordSysConvertToMap(GeometryPoint tempPoint)
        {
            using (GeoPoint point = new GeoPoint(tempPoint.STX, tempPoint.STY))
            {
                // 地图 经纬度 转换为投影坐标
                CoordSysTranslator.Convert(point, ProjectCoordSysType.LatLonPrjCoordSys, ProjectCoordSysType.MapPrjCoordSys, new CoordSysTransParameter(), CoordSysTransMethod.CoordinateFrame);

                return new GeometryPoint(point.X, point.Y);
            }
        }


        /// <summary>
        /// 获取 所有基站的凹凸闭包
        /// </summary>
        /// <param name="tempPoints">所有基站集合</param>
        /// <returns>外接多边形</returns>
        public List<GeometryPoint> GetPologyByPoints(List<Model.SiteNew> tempPoints)
        {
            List<GeometryPoint> returnGeometryPoint = new List<GeometryPoint>();
            Point2Ds point2Ds = new Point2Ds();

            foreach (var siteNew in tempPoints)
            {
                point2Ds.Add(new Point2D(siteNew.Longitude, siteNew.Latitude));
            }

            using (GeoRegion region = Geometrist.ComputeConvexHull(point2Ds))
            {
                if (region != null)
                {
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < region.PartCount; i++)
                    {
                        Point2Ds tempPoint2Ds = region[i];
                        foreach (Point2D point2D in tempPoint2Ds)
                        {
                            returnGeometryPoint.Add(new GeometryPoint(point2D.X, point2D.Y));
                            builder.AppendFormat("{0},{1}|", point2D.X, point2D.Y);
                        }
                    }

                    logger.Info(builder);
                }
            }

            return returnGeometryPoint;
        }

    }
}
